//
//  YLYbudgetViewController.m
//  iEasyBill
//
//  Created by student on 14-6-3.
//  Copyright (c) 2014年 com.yly16. All rights reserved.
//

#import "YLYbudgetViewController.h"
#import "PICircularProgressView.h"

@interface YLYbudgetViewController ()
@property (strong , nonatomic)NSMutableArray *billList;

@end

@implementation YLYbudgetViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.billList = [[NSMutableArray alloc]init];
    NSMutableArray *typelist = [[NSMutableArray alloc]init];
    for (iBillType *atype in [[DatabaseManager shareManager]showAllBillType:NO]) {
        [typelist addObject:atype];
    }
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    year = [components year];
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];           //设定时间格式,这里可以设置成自己需要的格式
    NSDate *Startdate =[dateFormat dateFromString:[NSString stringWithFormat:@"%d-01-01 00:00:00",year]];
    NSDate *Enddate =[dateFormat dateFromString:[NSString stringWithFormat:@"%d-12-31 00:00:00",year]];
    for (int i = 0; i < typelist.count ; i++) {
        NSMutableDictionary *dictoinary = [[NSMutableDictionary alloc]init];
        
        NSMutableArray *list = [[DatabaseManager shareManager]showBillByDate:Startdate toDate:Enddate toType:[typelist objectAtIndex:i]];
        [dictoinary setObject:list forKey:@"list"];
        [dictoinary setObject:[typelist objectAtIndex:i] forKey:@"type"];
        float sum = 0.0;
        for (iBill *abill in list) {
            sum += abill.billMoney.floatValue;
        }
        [dictoinary setObject:@(sum) forKey:@"money"];
        
        [self.billList addObject:dictoinary];
    }

}
-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.billList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    NSDictionary *dic = [self.billList objectAtIndex:indexPath.row];
    iBillType *mytype = [dic objectForKey:@"type"];
    UIImageView *imageView = (UIImageView*)[cell viewWithTag:1];
    imageView.image = [UIImage imageWithData:mytype.typeImageData];
    
    UILabel *nameLanbel = (UILabel *)[cell viewWithTag:2];
    nameLanbel.text = mytype.typeName;
    
    UILabel *budgetLabel = (UILabel*)[cell viewWithTag:4];
    budgetLabel.text = [NSString stringWithFormat:@"¥%.2f",mytype.budgetMoney.floatValue];
    
    float shengyu = mytype.budgetMoney.floatValue - [[dic objectForKey:@"money"] floatValue];
    UILabel *shengyuLabel = (UILabel*)[cell viewWithTag:5];
    if (shengyu < 0) {
        [shengyuLabel setTextColor:[UIColor redColor]];
    }
    shengyuLabel.text = [NSString stringWithFormat:@"¥%.2f",shengyu];
    
    PICircularProgressView *picView = (PICircularProgressView*)[cell viewWithTag:3];
    picView.progress = shengyu/mytype.budgetMoney.floatValue;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //选中后的反显颜色即刻消失 一切操作结束才取消反映色
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSIndexPath *indexpath = [self.tableView indexPathForCell:sender];
    
    NSObject *NewVC = [segue destinationViewController];
    [NewVC setValue:[self.billList objectAtIndex:indexpath.row] forKey:@"rootDictionary"];
}



@end
